package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import connection.ServiceProxy;

/**
 * Servlet implementation class advertisement
 */
public class addAdvertisement extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addAdvertisement() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/addAdvertisement.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("In post");
		if(null != request.getParameter("submit") && request.getParameter("submit").equals("Add Advertisement")) {
			System.out.println("In post");
			String itemName = request.getParameter("itemName");
			String itemDescription = request.getParameter("itemDescription");
			String itemPrice = request.getParameter("itemPrice");
			String itemQuantity = request.getParameter("itemQuantity");
			String sellerInfo = request.getParameter("sellerInfo");
			
			String destination = null;
			String result = null;
			
			String message = null;
			boolean error = false;
			
			if(itemName.equals("")) {
				message = "Please provide an Item Name";
				error = true;
			}
			else if(itemDescription.equals("")){
				message = "Please provide an Item Description";
				error = true;
			}
			else if(itemPrice.equals("")){
				message = "Please provide an Item Price";
				error = true;
			}
			
			if(error == false) {
				try {
					Float.parseFloat(itemPrice);
				}
				catch(NumberFormatException e) {
					error = true;
					message = "Invalid Price Value";
				}
			}
			
			if(error == false && itemQuantity.equals("")){
				message = "Please provide an Item Quantity";
				error = true;
			}
			else if(error == false) {
				try {
					Integer.parseInt(itemQuantity);
				}
				catch(NumberFormatException e) {
					error = true;
					message = "Invalid Quantity Value";
				}
			}
			
			if(error == false) {
				
				HttpSession session = request.getSession();
				String userId = (String)session.getAttribute("userId");
				ServiceProxy oProxy = new ServiceProxy();
				result = oProxy.addAdvertisement(userId, itemName, itemDescription, itemPrice, itemQuantity, sellerInfo);
				
				if(result.equals("true")) {
					message = "Advertisement Listed Successfully";
					destination = "/home";
				}
				else {
					error = true;
					message = "Please try again";
				}
				
				
			}
			
			request.setAttribute("message", message);
			
			if(error == true) {
				destination = "/addAdvertisement.jsp";
			}
			else
				destination = "/ViewMyAdvertisement";
			
			RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
			rd.forward(request, response);
		}
	}

}
